import com.yoke.util.StringUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by jiangzeyin on 2017/7/8.
 */
public class Css {
    public static void main(String[] args) throws IOException {
        String s = "dsfsd/index.html";
        System.out.println(s.substring(s.indexOf("/")));
        // CSSOMParser cssomParser = new CSSOMParser();

        Connection connection = Jsoup.connect("http://mp.weixin.qq.com/s/7J13gEuhulXQyNtKDqkrUA");
        Document document = connection.get();
        Elements scripts = document.getElementsByTag("script");
        scripts.remove();
        Elements style = document.getElementsByTag("style");
        style.forEach(element -> {
            String data = element.data();
            if (StringUtil.IsEmpty(data)) {
                element.remove();
                return;
            }
            doCss(data);
        });
    }

    static HashMap<String, HashMap<String, String>> doCss(String data) {
        if (StringUtil.IsEmpty(data)) {
            return null;
        }
        HashMap<String, HashMap<String, String>> css = new HashMap<>();
        char[] chars = data.toCharArray();
        StringBuilder queryName = new StringBuilder();
        StringBuilder queryContext = new StringBuilder();
        int find_start_count = 0;
        int find_end_count = 0;
        for (char ch : chars) {
            if (ch == '{') {
                find_start_count++;
                if (find_start_count > 1)
                    queryContext.append(ch);
            } else if (ch == '}') {
                find_end_count++;
                if (find_end_count > 1)
                    queryContext.append(ch);
                if (find_start_count == find_end_count) {
                    System.out.println(queryName);
                    System.out.println(queryContext);
                    if (queryContext.indexOf("{") != -1) {

                    } else {
                        String[] y = StringUtil.StringToArray(queryContext.toString(), ";");
                        HashMap<String, String> hashMap = new HashMap<>();
                        for (int i = 0; i < y.length; i++) {

                        }
                        for (String item : y) {
                            System.out.println(item);
                            String[] lines = StringUtil.StringToArray(item, ":");
                            if (lines.length == 2)
                                hashMap.put(lines[0], lines[1]);
                            else if (lines.length == 3) {
                                if (lines[1].equals("data")) {
                                    hashMap.put(lines[0], lines[1] + ":" + lines[2]);
                                }
                            } else {
                                throw new RuntimeException(item + "  " + lines.length);
                            }
                        }
                        System.out.println(hashMap);
                    }
                    queryName = new StringBuilder();
                    queryContext = new StringBuilder();
                    System.out.println("----------------");
                    find_start_count = 0;
                    find_end_count = 0;
                }
            } else {
                if (find_start_count == 0)
                    queryName.append(ch);
                else {
                    queryContext.append(ch);
                }
            }
            //  System.out.println(ch);
        }
        return css;
    }
}
